package com.lxj.common.utils;

import java.util.List;

/**
 * 
 * ��ҳҳ�����. ����ݼ���ҳ��Ϣ��Page��ʵ����������ʾ��ҳ��Ϣ�Ļ���
 * 
 * 
 * <p>
 * <a href="Page.java.html"><i>�鿴Դ�ļ�</i></a>
 * </p>
 * 
 * @author ��鲨
 */
public class Page implements java.io.Serializable {

	private static final long serialVersionUID = 7373086610263678434L;

	public static final int DEFAULT_PAGE_SIZE = 20;

	public static final int MAX_PAGE_SIZE = 100;

	/**
	 * ÿҳ�ļ�¼��ʵ�ʼ�¼��С�ڻ������
	 */
	private int pageSize = DEFAULT_PAGE_SIZE;

	private String pageUrl;

	private int maxPage;

	private int offset;

	/**
	 * ��ǰҳ��һ���������ݿ��е�λ��
	 */
	private int start;

	/**
	 * ��ǰҳ��ļ�¼��avaCount <= pageSize
	 */
	private int avaCount;

	/**
	 * �ܼ�¼��
	 */
	private int totalSize;

	/**
	 * ��ǰҳ�д�ŵļ�¼
	 */
	private List list;

	/**
	 * ��ǰҳ��
	 */
	private int currentPageNumber;

	/**
	 * ��ҳ��
	 */
	private int totalPageCount;

	/**
	 * ���췽����ֻ�����ҳ
	 */
	public Page() {
		this(0, 0, 0, DEFAULT_PAGE_SIZE, null);
	}

	/**
	 * Ĭ�Ϲ��췽��
	 * 
	 * @param start
	 *            ��ҳ�������ݿ��е���ʼλ��
	 * @param avaCount
	 *            ��ҳ����������
	 * @param totalSize
	 *            ��ݿ����ܼ�¼����
	 * @param pageSize
	 *            ��ҳ����
	 * @param data
	 *            ��ҳ������
	 */
	public Page(int start, int avaCount, int totalSize, int pageSize, List data) {
		this.avaCount = avaCount;
		this.pageSize = pageSize;
		this.start = start;
		this.totalSize = totalSize;
		this.list = data;

		this.currentPageNumber = (start - 1) / pageSize + 1;
		this.totalPageCount = (totalSize + pageSize - 1) / pageSize; // ������ҳ��ܺã�
		if (totalSize == 0 && avaCount == 0) {
			this.currentPageNumber = 1;
			this.totalPageCount = 1;
		}
	}

	/**
	 * ȡ��ҳ�����������ҳ�ܰ�ļ�¼��
	 */
	public int getPageSize() {
		return this.pageSize;
	}

	/**
	 * �Ƿ�����һҳ
	 */
	public boolean hasNextPage() {
		return (this.getCurrentPageNumber() < this.getTotalPageCount());
	}

	/**
	 * �Ƿ�����һҳ
	 */
	public boolean hasPreviousPage() {
		return (this.getCurrentPageNumber() > 1);
	}

	/**
	 * ��ȡ��ǰҳ��һ���������ݿ��е�λ��
	 */
	public int getStart() {
		return start;
	}

	/**
	 * ��ȡ��ǰҳ���һ���������ݿ��е�λ��
	 */
	public int getEnd() {
		int end = this.getStart() + this.getSize() - 1;
		if (end < 0)
			end = 0;
		return end;
	}

	/**
	 * ��ȡ��һҳ��һ���������ݿ��е�λ��
	 */
	public int getStartOfPreviousPage() {
		return Math.max(start - pageSize, 1);
	}

	/**
	 * ��ȡ��һҳ��һ���������ݿ��е�λ��
	 */
	public int getStartOfNextPage() {
		return start + avaCount;
	}

	/**
	 * ��ȡ��һҳ��һ���������ݿ��е�λ�ã�ÿҳ����ʹ��Ĭ��ֵ
	 */
	public static int getStartOfAnyPage(int pageNumber) {
		return getStartOfAnyPage(pageNumber, DEFAULT_PAGE_SIZE);
	}

	/**
	 * ��ȡ��һҳ��һ���������ݿ��е�λ��
	 */
	public static int getStartOfAnyPage(int pageNumber, int pageSize) {
		int startIndex = (pageNumber - 1) * pageSize + 1;
		if (startIndex < 1)
			startIndex = 1;
		return startIndex;
	}

	/**
	 * ȡ��ҳ��ļ�¼��
	 */
	public int getSize() {
		return avaCount;
	}

	/**
	 * ȡ��ݿ��а���ܼ�¼��
	 */
	public int getTotalSize() {
		return this.totalSize;
	}

	/**
	 * ȡ��ǰҳ��
	 */
	public int getCurrentPageNumber() {
		return this.currentPageNumber;
	}

	/**
	 * ȡ��ҳ��
	 */
	public int getTotalPageCount() {
		return this.totalPageCount;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

}
